// @ts-ignore
import { request, getOfficialPreview } from '@inbiz/utils';
export type CustomIconType = {
  Id: string;
  AppId: string;
  IconGroupOutId: string;
  Name: string;
  Path: string;
};

export type IconGroupType = {
  Id: string;
  OutId: string;
  AppId: string;
  IsDefault: boolean;
  Name: string;
};

export type UploadIconType = {
  iconGroupOutId: string;
  name: string;
  iconFile: string;
};

export type SerchDataType = {
  AppId: string;
  IconDtos: CustomIconType[];
  Id: string;
  IsDefault: boolean;
  Name: string;
  OutId: string;
};
// import { Form } from '@inbiz/antd';

// 获取所有的分组
export async function allGroups(): Promise<string> {
  return await request.get(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/iconGroup`,
  );
}

// 新增分组
export async function addGrop(params: { name: string }): Promise<string> {
  return await request.post(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/iconGroup`,
    {
      data: params,
    },
  );
}

// 删除分组
export async function deleteGrop(params: { Id: string }): Promise<string> {
  return await request.delete(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/iconGroup/${params.Id}`,
  );
}

// 上传图标
export async function uploadIcon(params: any): Promise<String> {
  const formData = new FormData();
  // 不走裁剪的图标类型
  // if (
  //   params.iconFile.file.type === 'image/svg+xml' ||
  //   params.iconFile.file.type === 'image/gif' ||
  //   params.iconFile.file.type === 'image/png'
  // ) {
  //   formData.append('iconFile', params.iconFile.file);
  // } else {
  //   formData.append('iconFile', params.iconFile.file.originFileObj);
  // }
  formData.append('iconFile', params.iconFile.file);
  formData.append('iconGroupOutId', params.iconGroupOutId);
  formData.append('name', params.name);

  return await request.post(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/icon`,
    {
      // Headers: {
      //   'Content-Type': 'multipart/form-data',
      // },
      data: formData,
    },
  );
}

// 根据搜索条件返回图标（搜索条件不能为空）
export async function getCustomIConBySearch(params: { serchText: string }): Promise<string> {
  return await request.get(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/icon/searchList?searchText=${params.serchText}`,
  );
}

// 根据分组outIds返回图标（搜索条件可为空）
export async function getCustomIConByGroup(params: {
  outId: string[] | string;
  serchText?: string;
}): Promise<string[]> {
  let engines = `engines`;
  if (getOfficialPreview()) {
    engines = `front/engines`;
  }
  const result = await Promise.all(
    (Array.isArray(params.outId) ? params.outId : [params.outId]).map((outId) =>
      request.get(
        `/inbiz/api/services/${engines}/v${InbizConfig.API_VERSION
        }/iconGroup/iconList/${outId}?searchText=${params.serchText || ''}`,
      ),
    ),
  );
  return result;
}

// 根据Id获取图标
export async function getCustomIconById(params: { Id: string }): Promise<string> {
  return await request.get(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/icon/${params.Id}`,
  );
}

// 删除图标
export async function deleteIconById(params: { Id: string }): Promise<string> {
  return await request.delete(
    `/inbiz/api/services${getOfficialPreview() ? '/front' : ''}/engines/v${InbizConfig.API_VERSION
    }/icon/${params.Id}`,
  );
}
